/**
 * 作者: hxl
 * 日期: 2025-04-24 10:05:27
 * 备注: 用户联盟乱斗信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.UnionManager;
import com.motu.monstercity.server.game.logic.UnionScuffleManager;
import com.motu.vertx.module.utility.model.EntityManager;

public class UserUnionScuffle extends UserUnionScuffleModel {

    public static final int MATCH_TYPE_RANDOM = 0; // 随机匹配
    public static final int MATCH_TYPE_CHALLENGE = 1; // 下战书


    public UserUnionScuffle() {
        super();
    }
    public UserUnionScuffle(long userId, long actId, long enemyUnionId) {
        putUserId(userId);
        putActId(actId);
        putEnemyUnionId(enemyUnionId);
        String actListKey = EntityManager.getFieldJoinString(userId, actId);
        putActListKey(actListKey);
    }

    public void resetBattle() {
        putDefeatedMembers("");
        putChooseList("");
        putFailedMembers("");
        putAddPerType(0);
        putWiningStreak(0); // 重置连胜次数
        update();
    }

    /**
     * 判断连胜次数
     */
    public void checkWiningStreak() {
        if (curHp <= 0 || winingStreak < 3) { // 如果已经死亡，则直接返回
            return;
        }
        long addHp = initHp * AllParam.UNION_SCUFFLE_WINING_STREAK_RECOVER_PER / 10000;
        long hp = curHp + addHp;
        hp = Math.min(hp, initHp);
        putCurHp(hp);
        putWiningStreak(0); // 重置连胜次数
    }

    public Common.PBUserUnionScuffle.Builder takeInitBuilder() {
        Common.PBUserUnionScuffle.Builder sb = super.takeInitBuilder();
        if (enemyUnionId > 0) {
            UnionInfo unionInfo = UnionManager.getUnionInfo(enemyUnionId);
            if (unionInfo != null) {
                sb.setEnemyUnionName(unionInfo.getName());
                sb.setEnemyUnionIcon(unionInfo.getIcon());
                sb.setEnemyUnionLevel(unionInfo.getLevel());
                int memberCount = UnionScuffleManager.getMemberCount(userId, actId, enemyUnionId);
                sb.setEnemyUnionMemberCount(memberCount);
            }
        }
        return sb;
    }
    public Common.PBUserUnionScuffle.Builder takeBuilder() {
        Common.PBUserUnionScuffle.Builder sb = super.takeBuilder();
        if (isFieldUpdate("enemyUnionId")) {
            if (enemyUnionId > 0) {
                UnionInfo unionInfo = UnionManager.getUnionInfo(enemyUnionId);
                if (unionInfo != null) {
                    sb.setEnemyUnionName(unionInfo.getName());
                    sb.setEnemyUnionIcon(unionInfo.getIcon());
                    sb.setEnemyUnionLevel(unionInfo.getLevel());
                    int memberCount = UnionScuffleManager.getMemberCount(userId, actId, enemyUnionId);
                    sb.setEnemyUnionMemberCount(memberCount);
                }
            } else {
                sb.setEnemyUnionName("");
                sb.setEnemyUnionIcon("");
            }
        }
        if (sb.build().toByteArray().length == 0) {
            return takeInitBuilder();
        }
        return sb;
    }


}
